package com.itwmk.service.impl.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itwmk.entity.Setmeal;
import com.itwmk.entity.SetmealDish;
import com.itwmk.entity.SetmealDto;
import com.itwmk.mapper.SetmealMapper;
import com.itwmk.service.impl.SetmealDishService;
import com.itwmk.service.impl.SetmealServic;
import com.itwmk.utils.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wmk
 */
@Transactional
@Service
public class SetmealImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealServic {

    @Autowired
    private SetmealDishService setmealDishService;
    @Override
    public void addSetmealWiteDish(SetmealDto dto) {
        save(dto);

        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        for (SetmealDish s:setmealDishes) {
            s.setSetmealId(dto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public void removeWithDish(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId,ids);
        wrapper.eq(Setmeal::getStatus,1);
        int count = count(wrapper);
        if (count>0){
            throw new CustomException("套餐正在启售，请禁用后再删除");
        }

        removeByIds(ids);
        LambdaQueryWrapper<SetmealDish> dishWrapper = new LambdaQueryWrapper<>();
        dishWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(dishWrapper);
    }
}
